#include "Sprites.h"
LPD3DXSPRITE Sprites::_spriteHandler;// de cho nhung dong phia duoi hieu dc. tai chua co khoi tao ji het
Sprites::Sprites(LPDIRECT3DTEXTURE9 image,bool loop,int index,int count,int row,int width,int height,int left,int top)
{
	_loop = loop;
	_index = index;
	_count = count;
	_row = row;
	_width = width;
	_height = height;
	_image = image;
	_left = left;
	_top = top;
}
void Sprites::Draw(int VirtualLeft,int StyleDraw,bool isClear)
{
	RECT rec;
	rec.left = _index*_width;
	rec.top = _row;
	rec.right = rec.left + _width;
	rec.bottom = rec.top+_height;
	_spriteHandler->Begin(D3DXSPRITE_ALPHABLEND);
	D3DXVECTOR3 position((float)(_left), (float)_top,0);
	_spriteHandler->Draw(_image,&rec,NULL,&position,D3DCOLOR_XRGB(100,255,255));
	_spriteHandler->End();
}
void Sprites::Draw()
{
	RECT rec;
	rec.left = _index*_width;
	rec.top = _row;
	rec.right = rec.left + _width;
	rec.bottom = rec.top+_height;
	_spriteHandler->Begin(D3DXSPRITE_ALPHABLEND);
	D3DXVECTOR3 position((float)(_left), (float)_top,0);
	_spriteHandler->Draw(_image,&rec,NULL,&position,D3DCOLOR_XRGB(100,255,255));
	_spriteHandler->End();
}
LPDIRECT3DTEXTURE9 Sprites::LoadTexture(LPCSTR filePath, D3DCOLOR transcolor)
{
	LPDIRECT3DTEXTURE9 textture = NULL;
	D3DXIMAGE_INFO info;
	LPDIRECT3DDEVICE9 d3ddv;
	_spriteHandler->GetDevice(&d3ddv);
	HRESULT result = D3DXGetImageInfoFromFileA(filePath,&info);
	if(result != D3D_OK)
	{
		return NULL;
	}
	result = D3DXCreateTextureFromFileExA(d3ddv,filePath,info.Width,info.Height,1,D3DPOOL_DEFAULT,D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,transcolor,&info,NULL,&textture);
	if(result != D3D_OK)
	{
		return NULL;
	}
	return textture;
}